//
//  NewAdvisoryViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/10.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

struct Doctor: HandyJSON {
    /// 医生id
    var id: String?
    /// 医生姓名
    var docName: String?
    /// 科室名称
    var deptName: String?
    /// 职位
    var jobTitleName: String?
    /// 诊费
    var fee: String?
    /// 专长
    var docGoodAtNames: String?
    var docGoodAt: String?
    /// 当前咨询人数
    var num: Int = 0
    
    /// 医院
    var hospitalName: String?
    var url: String?
    var thumbnailUrl: String?
    var score: String?
    var bucket: String?
}

struct DoctorViewModel {
    let models: [Doctor]
    
    var numberOfRows: Int { return models.count }
    
    func viewModel(for indexPath: IndexPath) -> RowViewModel {
        return RowViewModel(model: models[indexPath.row])
    }
    
    struct RowViewModel {
        let model: Doctor
        
        var doctorId: String { return model.id ?? "" }
        var doctorName: String { return model.docName ?? "" }
        var departmentName: String { return model.deptName ?? "" }
        /// 医生职位
        var office: String { return model.jobTitleName ?? "" }
        /// 咨询费
        var cost: String { return model.fee ?? "" }
        /// 医生专长
        var specialty: String { return "擅长：\(model.docGoodAtNames ?? "")" }
        /// 排队人数
        var peopleCount: String { return "\(model.num)" }
        var hospitalName: String { return model.hospitalName ?? "" }
        
        var thumbnailAvatar: String { return model.thumbnailUrl ?? "" }
        var originalAvatar: String { return model.url ?? "" }
        
        var cellHeight: CGFloat {
            let width: CGFloat = 48 + 15 * 5
            let defaultH: CGFloat = 148
            return defaultH + specialty.height(font: UIFont.medium(14), width: screenW - width)
        }
    }
}

extension DoctorViewModel {
    static func getDoctorList(departmentId: String,
                              success: @escaping (_ models: [Doctor]) -> Void,
                              failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["id": departmentId]
        HttpTool.shared.request(target: ApiManager.doctorList(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let models = JSONDeserializer<Doctor>.deserializeModelArrayFrom(json: json["data"].description) as? [Doctor] {
                    success(models)
                } else {
                    failure("请求数据发生错误，稍候重试!")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
}
